import ast

from appium import webdriver
from appium.options.android import UiAutomator2Options

from config.config import LOCATOR_CONF_PATH
from utils.locate_ele import locate_element
from utils.ymal_reader import YamlReader

'''
登陆页面
'''


class LoginPage:

    def __init__(self, driver):
        self.element = YamlReader.yaml_reader(LOCATOR_CONF_PATH + '/login_locator.yaml')
        self.driver = driver

    def myButton_click(self):
        ele = self.element['MY_BUTTON'].split(',')
        locate_element(self.driver, ele[0], ele[1]).click()

    def loginOrRegisterButton_click(self):
        ele = self.element['LOGINORRGISTER_BUTTON'].split(',')
        locate_element(self.driver, ele[0], ele[1]).click()

    def typeOfLoginButton_click(self):
        ele = self.element['TYPEOF_LOGIN_BUTTON'].split(',')
        locate_element(self.driver, ele[0], ele[1]).click()

    def phoneTextbox_input(self, phone_num):
        ele = self.element['PHONE_TEXTBOX'].split(',')
        locate_element(self.driver, ele[0], ele[1]).send_keys(phone_num)

    def phoneTextbox_clear(self):
        ele = self.element['PHONE_TEXTBOX'].split(',')
        locate_element(self.driver, ele[0], ele[1]).clear()


    def passwordTextbox_input(self, pw_num):
        ele = self.element['PASSWORD_TEXTBOX'].split(',')
        locate_element(self.driver, ele[0], ele[1]).send_keys(pw_num)

    def passwordTextbox_clear(self):
        ele = self.element['PASSWORD_TEXTBOX'].split(',')
        locate_element(self.driver, ele[0], ele[1]).clear()

    def agreeButton_click(self):
        ele = self.element['AGREE_BUTTON'].split(',')
        locate_element(self.driver, ele[0], ele[1]).click()

    def loginButton_click(self):
        ele = self.element['AGREE_BUTTON'].split(',')
        locate_element(self.driver, ele[0], ele[1]).click()


if __name__ == "__main__":
    desired_caps = {
        "platformName": "Android",
        "deviceName": "Q7PDU19731008305",
        "appPackage": "com.sankuai.movie",
        "appActivity": "com.sankuai.movie.MovieMainActivity",
        "automationName": "UiAutomator2",
        # "noReset": True
    }

    print("Desired Capabilities: ", desired_caps)

    driver = webdriver.Remote("http://localhost:4723", options=UiAutomator2Options().load_capabilities(desired_caps))

    loginPage = LoginPage(driver)

    loginPage.myButton_click()
